<?php

namespace BlogBundle\Controller;

use BlogBundle\Entity\Article;
use BlogBundle\Form\ArticleType;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Form\Form;

class DefaultController extends Controller
{
    /**
     * @Route("/blog",name="blog")
     */
    public function indexAction(Request $request)
    {

        $formData=new Article();
        $form = $this->createForm(ArticleType::class,$formData);

        if($request->getMethod() == "POST"){

            $form->submit($request->request->get($form->getName()));
            if($form->isSubmitted() && $form->isValid()){

                return $this->redirectToRoute('success');
            }else{
                throw $this->createAccessDeniedException();
            }
        }



        return $this->render('pages/index.html.twig',array('form'=>$form->createView(),'list'=>$formData));
    }

    /**
     * @Route("/success",name="success")
     */
    public function successAction(Request $request){
        return $this->render('blog/success.html.twig');
    }
}
